import os
import re
libContent=[]
count=0

with open("yueduLib","r",encoding="utf8") as F:
    for i in F.readlines():
        thisLine=i.strip("\n")
        if thisLine.isdigit():
            count=int(thisLine)-1
            libContent.append([])
        else:
            libContent[count].append(thisLine)
#print(libContent)
print(libContent)
exit()
import sys
from urllib import request
from html import parser
def getUrl(bookname):
    if bookname=="六祖坛经":
        getHandle = request.quote("谈谈《六祖坛经》与生活禅")  # unquote
    else:
        getHandle=request.quote(bookname+" 800")  #unquote
    url = "http://so.yjbys.com/cse/search?q="+getHandle+"&s=13920434325658367796&nsid=0"
    print(url)
    wp=request.urlopen(url)
    content=wp.read()
    content=str(content,encoding="utf8")
    return content


try:
    os.mkdir("read")
except FileExistsError:
    pass
#print(libContent)
def createFile():
    os.chdir("read")
    print(os.getcwd())
    for index,dirname in enumerate(libContent):
        try:
            os.mkdir(str(index+1))
        except FileExistsError:
            pass
        os.chdir(str(index+1))
        print(os.getcwd())
        for file in libContent[index]:
            with open(file+".xml","w",encoding="utf8") as F:
                ##开始爬网站
                cont=getUrl(file)   #拿到搜索页面的内容
                if file == "六祖坛经":
                    tagMatch = re.findall(".*cpos=\"title\".*谈谈《六祖坛经》与生活禅.*", cont)  # 查找到第一个搜索内容
                else:
                    tagMatch = re.findall(".*cpos=\"title\".*800.*", cont) #查找到第一个搜索内容
                if tagMatch:
                    contentUrl=getContentUrl(tagMatch[0])
                    content=linkContentUrl(contentUrl)


                    #获取所有汉字
                    contentGroup=content.split("\n")
                    fileText=""
                    for cont in contentGroup:
                        if cont:
                            CHcont=re.search("[\u2E80-\uFFFD]+",cont)
                            if CHcont:
                                fileText+=CHcont.group()
                    print(fileText)
                    #####格式化放入
                    print(os.getcwd())
                    wordContent=changeWord(
                        title=fileText[0:10],
                        bookname=file,
                        content=fileText,
                        year=str(2013+int(index)/2),
                        month=str(index+1),
                        day=str(index+1),
                    )
                    F.write(wordContent)
                #############
        os.chdir("../")

def changeWord(title,bookname,content,year,month,day):
    f=open("../../model.xml","r",encoding="utf-8")
    xmlContent=f.read()

    titleHd=title
    xmlContent=re.sub("\{title\}",titleHd,xmlContent)
    booknameHd = bookname
    xmlContent = re.sub("\{bookname\}", booknameHd, xmlContent)
    contentHd = content
    xmlContent = re.sub("\{content\}", contentHd, xmlContent)
    yearHd = year
    xmlContent = re.sub("\{year\}", yearHd, xmlContent)
    monthHd = month
    xmlContent = re.sub("\{month\}", monthHd, xmlContent)
    dayHd = day
    xmlContent = re.sub("\{day\}", dayHd, xmlContent)
    # print(xmlContent)
    f.close()
    return xmlContent










def getContentUrl(match):
    print(match)
    con=re.search("href=\"(.*)\" ", match).group(1)

    return con
def linkContentUrl(url):
    print(url)
    wp=request.urlopen(url)
    content=str(wp.read(),encoding="gbk")

    return content


createFile()
